package com.net.oil.news.adapter;
/*
 * Created by lch on 2018/3/4 0004.
 */

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.http.AppUrl;
import com.net.oil.news.NewsDetailActivity;
import com.net.oil.news.bean.NewsListBean;
import com.net.oil.util.DateFormat;
import com.net.oil.util.StringUtil;
import com.net.oil.util.glide.GlideUtil;
import com.net.oil.webview.WebViewActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NewsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_FOOTER = 0;  //底部FootView
    private static final int TYPE_ITEM = 1;  //普通item

    private int load_more_status = 10;      //上拉加载更多状态-默认为10

    private List<NewsListBean.NewsBean> newsBeanList;

    private Context context;
    private LayoutInflater inflater;

    public NewsListAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        newsBeanList = new ArrayList<>();
    }

    public void updateData(List<NewsListBean.NewsBean> newsBeanList) {
        if (this.newsBeanList != null) {
            this.newsBeanList.clear();
        } else {
            this.newsBeanList = new ArrayList<>();
        }
        this.newsBeanList.addAll(newsBeanList);
        notifyDataSetChanged();
    }

    public void loadMore(List<NewsListBean.NewsBean> newsBeanList) {
        if (this.newsBeanList == null) {
            this.newsBeanList = new ArrayList<>();
        }
        this.newsBeanList.addAll(newsBeanList);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE_ITEM:
                view = inflater.inflate(R.layout.news_lsit_recycler_view_item, parent, false);
                return new ViewHolder(view);
            case TYPE_FOOTER:
                view = inflater.inflate(R.layout.recycler_view_foot_item, parent, false);
                return new FootViewHolder(view);
            default:
                return null;
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            itemDataBind(viewHolder, position);
        } else if (holder instanceof FootViewHolder) {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            footDataBind(footViewHolder);
        }
    }

    private void itemDataBind(ViewHolder viewHolder, int position) {
        final NewsListBean.NewsBean news = newsBeanList.get(position);
        if (news != null) {
            if (!StringUtil.isEmpty(news.getTitle())) {
                viewHolder.newsTitle.setText(news.getTitle());
            }
            viewHolder.newsTime.setText(DateFormat.formatDateTime(news.getAddTime(), DateFormat.FormatFlag.YDM));

            if (news.getReadCount() != null) {
                viewHolder.newsReadCount.setText(String.format(context.getString(R.string.read_format), news.getReadCount()));
            }

            if (!StringUtil.isEmpty(news.getImage())) {
                GlideUtil.getInstance().loadImage(context, viewHolder.newsImage, AppUrl.getImageUrl(news.getImage()), R.drawable.news_err);
            }

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //如果这个字段不为空就用这个web加载这个URL，如果为空就加载content中的html文本，新闻详情和技术支援详情采用这种方式加载，
                    // 活动详情不变还是加载html文本

                    if (!StringUtil.isEmpty(news.getOpenUrl())) {
                        Intent intent = new Intent(context, WebViewActivity.class);
                        intent.putExtra(Constant.INTENT_TO_WEBVIEW_URL_KEY, news.getOpenUrl());
                        context.startActivity(intent);
                    } else {
                        Intent intent = new Intent(context, NewsDetailActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(Constant.INTENT_NEWS_KEY, news);
                        intent.putExtras(bundle);
                        context.startActivity(intent);
                    }

                }
            });
        }
    }


    @Override
    public int getItemCount() {
        if (newsBeanList != null) {
            return newsBeanList.size() + 1;
        } else {
            return 0;
        }
    }

    /**
     * 更改滑动的状态
     *
     * @param status 上拉加载更多 PULL_LOAD_MORE = 10; 正在加载中 LOADING_MORE = 11;
     *               没有更多 LOADING_NONE = 12; 上拉加载更多状态-默认为10
     */
    public void changeMoreStatus(int status) {
        load_more_status = status;
        notifyDataSetChanged();
    }

    /**
     * 绑定加载更多项的数据
     *
     * @param footViewHolder holder
     */
    private void footDataBind(FootViewHolder footViewHolder) {
        switch (load_more_status) {
            case Constant.PULL_LOAD_MORE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("上拉加载更多...");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
            case Constant.LOADING_MORE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("正在加载更多数据...");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.VISIBLE);
                break;
            case Constant.LOADING_NONE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("没有更多了");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
            case Constant.LOADING_FAIL:
                footViewHolder.more.setText("加载失败");
                footViewHolder.more.setEnabled(true);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.news_image)
        ImageView newsImage;
        @BindView(R.id.news_title)
        TextView newsTitle;
        @BindView(R.id.news_time)
        TextView newsTime;
        @BindView(R.id.news_read_count)
        TextView newsReadCount;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.progress_bar)
        ProgressBar progressBar;
        @BindView(R.id.more)
        TextView more;

        FootViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
